package com.yf.course.modules.course.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.ability.log.annon.LogInject;
import com.yf.ability.log.enums.LogType;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.BaseIdReqDTO;
import com.yf.base.api.api.dto.BaseIdsReqDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.course.modules.course.dto.CourseQaDTO;
import com.yf.course.modules.course.dto.request.CourseQaReqDTO;
import com.yf.course.modules.course.dto.response.CourseQaRespDTO;
import com.yf.course.modules.course.service.CourseQaService;
import com.yf.system.modules.user.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* 课程问答控制器
* </p>
*
* @author 聪明笨狗
* @since 2021-04-19 11:04
*/
@Api(tags={"课程问答"})
@RestController
@RequestMapping("/api/course/qa")
public class CourseQaController extends BaseController {

    @Autowired
    private CourseQaService baseService;

    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */
    @LogInject(title="回复课件问答", logType = LogType.COURSE)
    @ApiOperation(value = "添加或修改")
    @PostMapping("/save")
    public ApiRest save(@RequestBody CourseQaDTO reqDTO) {
        baseService.save(reqDTO);
        return super.success();
    }

    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @RequiresPermissions(value = {"course:qa:delete"})
    @ApiOperation(value = "批量删除")
    @PostMapping("/delete")
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "查找详情")
    @PostMapping("/detail")
    public ApiRest<CourseQaRespDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        CourseQaRespDTO dto = baseService.detail(reqDTO.getId());
        return super.success(dto);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public ApiRest<IPage<CourseQaRespDTO>> paging(@RequestBody PagingReqDTO<CourseQaReqDTO> reqDTO) {


        // 我的
        CourseQaReqDTO params = reqDTO.getParams();
        if(params != null && params.getIsMine()!=null && params.getIsMine()){
            params.setCreateBy(UserUtils.getUserId());
        }

        //分页查询并转换
        IPage<CourseQaRespDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }

}
